package com.golang.api.dto.response.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import com.golang.api.domain.OrderInfo;
import com.golang.api.domain.OrderItem;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@Slf4j
public class CreateOrderDto {

    //订单编号
    private String orderId;

    //下单时间
    private String orderTime;

    //订单总金额
    private BigDecimal totalAmount;


    private List<CreateOrderItemDto> itemList;




    public static CreateOrderDto convertDTO(OrderInfo orderInfo, List<OrderItem> list){
        if(Objects.nonNull(orderInfo) && CollUtil.isNotEmpty( list)){
            try{
                CreateOrderDto dto = new CreateOrderDto();
                dto.setOrderId(orderInfo.getOrderId());
                dto.setTotalAmount(orderInfo.getTotalAmount());
                dto.setOrderTime(orderInfo.getCreateTime().format(DatePattern.NORM_DATE_FORMATTER));
                dto.setItemList(list.stream().map(CreateOrderItemDto::convertDTO).collect(Collectors.toList()));
                return dto;
            }catch(Exception e) {
                log.error("orderInfo 转 CreateOrderDto 出错,错误信息:{}",e.getMessage(),e);
            }
        }
        return null;
    }


}
